<?php

/**
 * Essay form base class.
 *
 * @method Essay getObject() Returns the current form's model object
 *
 * @package    quotes
 * @subpackage form
 * @author     Your name here
 */
abstract class BaseEssayForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                        => new sfWidgetFormInputHidden(),
      'author_id'                 => new sfWidgetFormPropelChoice(array('model' => 'Author', 'add_empty' => true)),
      'title'                     => new sfWidgetFormInputText(),
      'content'                   => new sfWidgetFormTextarea(),
      'is_active'                 => new sfWidgetFormInputCheckbox(),
      'created_at'                => new sfWidgetFormDateTime(),
      'updated_at'                => new sfWidgetFormDateTime(),
      'slug'                      => new sfWidgetFormInputText(),
      'essay_essay_category_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'EssayCategory')),
    ));

    $this->setValidators(array(
      'id'                        => new sfValidatorChoice(array('choices' => array($this->getObject()->getId()), 'empty_value' => $this->getObject()->getId(), 'required' => false)),
      'author_id'                 => new sfValidatorPropelChoice(array('model' => 'Author', 'column' => 'id', 'required' => false)),
      'title'                     => new sfValidatorString(array('max_length' => 200)),
      'content'                   => new sfValidatorString(),
      'is_active'                 => new sfValidatorBoolean(),
      'created_at'                => new sfValidatorDateTime(array('required' => false)),
      'updated_at'                => new sfValidatorDateTime(array('required' => false)),
      'slug'                      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'essay_essay_category_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'EssayCategory', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(array(
        new sfValidatorPropelUnique(array('model' => 'Essay', 'column' => array('title'))),
        new sfValidatorPropelUnique(array('model' => 'Essay', 'column' => array('slug'))),
      ))
    );

    $this->widgetSchema->setNameFormat('essay[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Essay';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['essay_essay_category_list']))
    {
      $values = array();
      foreach ($this->object->getEssayEssayCategorys() as $obj)
      {
        $values[] = $obj->getEssayCategoryId();
      }

      $this->setDefault('essay_essay_category_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveEssayEssayCategoryList($con);
  }

  public function saveEssayEssayCategoryList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['essay_essay_category_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(EssayEssayCategoryPeer::ESSAY_ID, $this->object->getPrimaryKey());
    EssayEssayCategoryPeer::doDelete($c, $con);

    $values = $this->getValue('essay_essay_category_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new EssayEssayCategory();
        $obj->setEssayId($this->object->getPrimaryKey());
        $obj->setEssayCategoryId($value);
        $obj->save();
      }
    }
  }

}
